<?php
	include('header.php');
?>		
<script>
	
	$(function() {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
		
		$("#tdnNo").focus();
		
		var name = $( "#name" ),
			allFields = $( [] ).add( name ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( "Length of " + n + " must be between " +
					min + " and " + max + "." );
				return false;
			} else {
				return true;
			}
		}
		
		function errorField( o, n ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
		}

		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
		
		
		$("#transportsch").change(function () {
			
			var str = "";
			$("select option:selected").each(function () {
					str =$(this).val();
			});
				var data = 'trnID='+str;
				$.ajax({
						url: "ajax/transportbydriver.php",  
						type: "GET",        
						data: data,     
						cache: false,
						success: function (result) {  
							if(result!='0'){
								$("#drivername").text(result);
							}
						}       
				});
		 
			$("#updateTdnNo").click();
        });
		
	
	});
	</script>


		<!--[if !IE]>start content<![endif]-->					
		<div id="content">
			<div class="inner">
				
				<!--[if !IE]>start section<![endif]-->
				<?php

				

				?>
		
				<div class="section">
					
					<!--[if !IE]>start title wrapper<![endif]-->
					<div class="title_wrapper">
						<span class="title_wrapper_top"></span>
						<div class="title_wrapper_inner">
							<span class="title_wrapper_middle"></span>
							<div class="title_wrapper_content"> 
								<h2>Package By Transport For Today (<?php echo date("l d-m-Y"); ?>)</h2>
							</div>
						</div>
						<span class="title_wrapper_bottom"></span>
					</div>
					<!--[if !IE]>end title wrapper<![endif]-->
					
					<!--[if !IE]>start section content<![endif]-->
					<div class="section_content">
						<span class="section_content_top"></span>
						
						<div class="section_content_inner">
							<?php
								 
								$tdnNo = "";  
								$transportsch = "";
								if(isset($_POST['reset'])){
									$_POST = "";
								}
								if(isset($_POST['transportsch'])){ 
									$transportID = mysql_real_escape_string($_POST['transportsch']); 
								}
								
								if(isset($_POST['tdnNo'])){ 
									$tdnNo = mysql_real_escape_string($_POST['tdnNo']); 
									$resultUpdateTdnNo = "";
									
									if($tdnNo != "" ) {
										$qTotTPD= "SELECT COUNT(*)  FROM transport_package_delivering 
														WHERE  
														tpd_tdn_no = '$tdnNo' 
														AND tpd_status = 'TPD00' 
														AND tpd_trn_id IS NULL 
													 ";
										$totalTPD = $db->sql_total($qTotTPD);
										
										
										if($totalTPD == 1) {
										
											$qTPD= "SELECT * FROM transport_package_delivering 
															WHERE  
															tpd_tdn_no = '$tdnNo' 
															AND tpd_status = 'TPD00' 
															AND tpd_trn_id IS NULL 
														 ";
											$resTPD = $db->sql_list($qTPD);
											
											$tpdID = $resTPD['tpd_id'];
											
											$tpd_status = 'TPD01';
											$sqlUpdPTD= "UPDATE  transport_package_delivering 
															set tpd_delivered_date = NOW(),
																tpd_trn_id = '$transportID',
																tpd_status = '$tpd_status',
																tpd_status_remarks = 'Start delivering',
																tpd_modified_date = now(),
																tpd_modified_by = '$_SESSION[log_userlogin_id]'
															WHERE 	
																tpd_id = '$tpdID' ";
											$resUpdPTD = $db->sql_query($sqlUpdPTD);
											
											$sqlInsertLog = " INSERT INTO tpd_status_log 
														(tsl_tpd_id,tsl_tdn_no,tsl_status,tsl_status_remarks,tsl_created_date,tsl_created_by) 
															VALUES
														('$tpdID','$tdnNo','$tpd_status','update status from initial to start delivering',NOW(),'$_SESSION[log_userlogin_id]')";
											$resInsertLog= $db->sql_query($sqlInsertLog);
											
											if($resUpdPTD && $resInsertLog)
												$resultUpdateTdnNo = "Update Successful for TDN N0. $tdnNo";
										}else {
											$resultUpdateTdnNo = "Search for '$tdnNo' not found!";
										}
									}
								}
								
								
								
								
								
								//use for pagination url parameter
								$param = "";
							?>
							<form action="packagebytransport.php" method="POST" class="search_form">
							
							
							<!--[if !IE]>start fieldset<![endif]-->
							<fieldset>
								<!--[if !IE]>start forms<![endif]-->
								<div class="forms">
								<?php
									$qCountUpl= "SELECT COUNT(*)  FROM transport_package_delivering 
												WHERE 
												tpd_delivered_date IS NULL
												AND tpd_upload_id  IS NOT NULL 
												AND tpd_trn_id  IS  NULL 
												 ";
									$totalUploads = $db->sql_total($qCountUpl);
								?>
								<h3>Total Uploads :   <?php  echo $totalUploads; echo ' '.$lang['CONTENT_RECORDS']?>  </h3>
								
								<?php /* Get data. */
												$qListTrn= "select * FROM transport t, person p, person_transport_delivering ptd
															WHERE
													ptd.ptd_trn_id = t.trn_id
													AND p.per_id = ptd.ptd_person_per_id
													AND ptd.ptd_date_assign_driver =  DATE_FORMAT(NOW(),'%y-%m-%d')
													order by t.trn_plate_no   ";
												$rListTrn = $db->sql_fetch($qListTrn);
								?>
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<label>Transport :</label>
									<div class="inputs">
										<span class="input_wrapper" style="width:300px;">
										<select name="transportsch" id="transportsch" >
										<?php 
											if($transportID == "")
												$transportID = $rListTrn[0]['trn_id'];
											foreach ($rListTrn as $i) {	
												echo "<option value='$i[trn_id]' "; 
													if($transportID==$i['trn_id']) 
														echo 'selected'; 
												echo ">$i[trn_plate_no]</option>";
											} ?>
										</select>
										
									</span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								<!--[if !IE]>start row<![endif]-->
								<div id="divdriver" class="row">
									<label>Driver :</label>
									<div class="inputs">
										<span id="drivername" class="input_wrapper" style="width:300px;">
											<?php 
												foreach ($rListTrn as $i) {	
													if($i['trn_id']==$transportID){
														echo $i['per_full_name'];
													}
												}
											?>
										</span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
						
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<label>TDN No. :</label>
									<div class="inputs">
										<span class="input_wrapper" style="width:300px;">
											<input  class="text" id="tdnNo" name="tdnNo" value="" type="text"  />
										</span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<div class="inputs">
										<span class="button blue_button search_button"><span><span><em>Update TDN No.</em></span></span><input id="updateTdnNo" name="updateTdnNo" type="submit" /></span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								
								
								</div>
								<!--[if !IE]>end forms<![endif]-->
								
							</fieldset>
							<!--[if !IE]>end fieldset<![endif]-->
							
							
							</form>
							<?php
								/* 
									   First get total number of rows in data table. 
									   If you have a WHERE clause in your query, make sure you mirror it here.
									*/
									
									if($transportID != ''){
										$addClause = " AND t.trn_id = '$transportID' ";
									}
									
									$qCount= "SELECT COUNT(*)  FROM transport t , transport_package_delivering tpd , entity s, entity r
												WHERE t.trn_id = tpd.tpd_trn_id
												AND tpd.tpd_sender_ent_id=s.ent_id
												AND tpd.tpd_receiver_ent_id=r.ent_id
												AND tpd_status = 'TPD01'
												AND tpd.tpd_delivered_date = DATE_FORMAT(NOW(),'%y-%m-%d') 
												$addClause ";
									$total_pages = $db->sql_total($qCount);
									/* Setup vars for query. */
									$limit = 10; 								//how many items to show per page
							
							?>
							<div class="table_tabs_menu">
								<h3><?php  
									//show status result
									echo $resultUpdateTdnNo; ?> </h3>
								<br />
								<a href="packageadd.php?trn_id=<?php echo $transportID; ?>" class="update"><span><span><em>Add Package</em><strong></strong></span></span></a>
								<a  target="_blank" href="printListPackageByTransport.php?trn_id=<?php echo $transportID; ?>" class="print"><span><span><em>CETAK</em><strong></strong></span></span></a>
							</div>
							
							<!--[if !IE]>start table_wrapper<![endif]-->
							<div class="table_wrapper">
								<div class="table_wrapper_inner">
								
								<?php  
									$page="";
									if(isset($_GET['page']))
										$page=$_GET['page'];
									
									if($page) 
										$start = ($page - 1) * $limit; 			//first item to display on this page
									else
										$start = 0;								//if no page var is given, set start to 0
									
									/* Get data. */
									$qList= "SELECT t.*, tpd.*, s.ent_id AS sid ,
											s.ent_short_name AS sname, 
											r.ent_id AS rid, r.ent_short_name AS rname, r.ent_add_postcode AS rpostcode
											FROM transport t , transport_package_delivering tpd , entity s, entity r
												WHERE t.trn_id = tpd.tpd_trn_id
												AND tpd.tpd_sender_ent_id=s.ent_id
												AND tpd.tpd_receiver_ent_id=r.ent_id
												AND tpd_status = 'TPD01'
												AND tpd.tpd_delivered_date = DATE_FORMAT(NOW(),'%y-%m-%d') 
												$addClause 
												ORDER BY tpd_modified_date desc, r.ent_add_postcode DESC
												LIMIT $start, $limit ";
									$rList = $db->sql_fetch($qList);			

								?>
								<table cellpadding="0" cellspacing="0" width="100%">
									<tbody>
									<tr>
										<th style="width: 10px;">Bil.</th>
										<th style="width: 50px;">TDN No.</th>
										<th style="width: 50px;">DO No.</th>
										<th style="width: 50px;">Quatity</th>
										<th>Sender</th>
										<th>Receiver</th>
										<th>Destination</th>
										<th>Status</th>
										<th>Action</th>
										
									</tr>
								<?php
									$num =$start;
									foreach ($rList as $i) {
										$num++;
										$colorRow = "first";
										if($num%2==0)
											$colorRow = "second"; 
										else
											$colorRow = "first";
										
										$qCity= "SELECT * from lookup_postcode where postcode = '$i[rpostcode]' ";
										$rCity = $db->sql_list($qCity);
										
										echo '	
											<tr class="'.$colorRow.'">
												<td>'.$num.'</td>
												<td>'.$i['tpd_tdn_no'].'</td>
												<td>'.$i['tpd_do_ref'].'</td>
												<td>'.$i['tpd_qty'].' '.$i['tpd_qty_type'].'</td>
												<td>'.$i['sname'].'</td>
												<td>'.$i['rname'].'</td>
												<td>'.$i['rpostcode'].' '.$rCity['post_office'].'</td>
												<td>'.getStatus($i['tpd_status']).'</td>
												<td>
													<div class="actions_menu" >
														<ul>
															<li><a class="details" href="packageview.php?id='.$i['tpd_id'].'">View</a></li>
															<li><a class="edit" href="packageupdate.php?id='.$i['tpd_id'].'">Update</a></li>
														</ul>
													</div>
												</td>
											</tr>';
										
									}
								?>
								</tbody></table>
								</div>
								</div>
								<?php
								if($total_pages > 0){
									echo '<h3>'.$total_pages.' '.$lang['CONTENT_RECORDS'].'</h3>';
								}
								?>
							</div><!--[if !IE]>end table_wrapper<![endif]-->
						
						<!--[if !IE]>start pagination<![endif]-->
							<div class="pagination_wrapper">
							<span class="pagination_top"></span>
							<div class="pagination_middle">
							<div class="pagination">
								<?php
								// How many adjacent pages should be shown on each side?
									$adjacents = 3;
								/* Setup page vars for display. */
									if ($page == 0) $page = 1;					//if no page var is given, default to 1.
									$prev = $page - 1;							//previous page is page - 1
									$next = $page + 1;							//next page is page + 1
									$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
									$lpm1 = $lastpage - 1;						//last page minus 1
								/* 
									Now we apply our rules and draw the pagination object. 
									We're actually saving the code to a variable in case we want to draw it more than once.
								*/
								echo '<span class="page_no">'.$lang['CONTENT_PAGINATION_PAGE'].' '.$page.' of '.$lastpage.' </span>';
								echo '<ul class="pag_list">';

									//previous button
									if ($page > 1) 
										echo '<li><a href="packagebytransport.php?page='.$prev.$param.'" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_PREVIOUS'].'</span></span></a> </li>';
									else
										echo '<li><a href="#" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_PREVIOUS'].'</span></span></a> </li>';

									//pages	
									if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
									{	
										for ($counter = 1; $counter <= $lastpage; $counter++)
										{
											if ($counter == $page)
												echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
											else	
												echo '<li><a href="packagebytransport.php?page='.$counter.$param.'">'.$counter.'</a></li>';	
										}
									}
									elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
									{
										//close to beginning; only hide later pages
										if($page < 1 + ($adjacents * 2))		
										{
											for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
											{
												if ($counter == $page)
													echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
												else	
													echo '<li><a href="packagebytransport.php?page='.$counter.$param.'">'.$counter.'</a></li>';													
											}
											echo '<li>[...]</li>';
											echo '<li><a href="packagebytransport.php?page='.$lpm1.$param.'">'.$lpm1.'</a></li>';	
											echo '<li><a href="packagebytransport.php?page='.$lastpage.$param.'">'.$lastpage.'</a></li>';		
										}
										//in middle; hide some front and some back
										elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
										{
											echo '<li><a href="packagebytransport.php?page=1'.$param.'">1</a></li>';	
											echo '<li><a href="packagebytransport.php?page=2'.$param.'">2</a></li>';
											echo '<li>[...]</li>';											
											for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
											{
												if ($counter == $page)
													echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
												else
													echo '<li><a href="packagebytransport.php?page='.$counter.$param.'">'.$counter.'</a></li>';						
											}
											echo '<li>[...]</li>';
											echo '<li><a href="packagebytransport.php?page='.$lpm1.$param.'">'.$lpm1.'</a></li>';	
											echo '<li><a href="packagebytransport.php?page='.$lastpage.$param.'">'.$lastpage.'</a></li>';												
										}
										//close to end; only hide early pages
										else
										{
											echo '<li><a href="packagebytransport.php?page=1'.$param.'">1</a></li>';	
											echo '<li><a href="packagebytransport.php?page=2'.$param.'">2</a></li>';
											echo '<li>[...]</li>';	
											for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
											{
												if ($counter == $page)
													echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
												else
													echo '<li><a href="packagebytransport.php?page='.$counter.$param.'">'.$counter.'</a></li>';						
											}
										}
									}
									//next button
									if ($page < $counter - 1) 
										echo '<li><a href="packagebytransport.php?page='.$next.$param.'" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_NEXT'].'</span></span></a> </li>';
									else
										echo '<li><a href="#" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_NEXT'].'</span></span></a> </li>';
									?>
								</ul>
							</div>
							</div>
							<span class="pagination_bottom"></span>
							</div>
						<!--[if !IE]>end pagination<![endif]-->
						
						
						<span class="section_content_bottom"></span>
					</div>
					<!--[if !IE]>end section content<![endif]-->
				</div>
				<!--[if !IE]>end section<![endif]-->
				
					
				
			</div>
		</div>
		<!--[if !IE]>end content<![endif]-->

<?php
	include('footer.php');
?>